package com.doge.controller.admin.promotion;

import com.doge.bean.base.BaseData;
import com.doge.bean.base.BaseParam;
import com.doge.bean.base.BaseRespVo;
import com.doge.bean.pojo.coupon.Coupon;
import com.doge.bean.pojo.coupon.CouponUser;
import com.doge.bean.vo.coupon.CouponGoodsVO;
import com.doge.service.admin.promotion.CouponService;
import com.doge.util.anno.MarketHibernateValid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * promotion包里面全是推广模块相关的
 * CouponController负责的是推广模块里面的优惠券管理
 * Created By ntian
 */
@RestController
@RequestMapping("admin/coupon")
public class CouponController {
    @Autowired
    CouponService couponService;

    /**
     * 优惠券列表，可以显示目前都有什么优惠券 GET
     * admin/coupon/list?page=1&limit=20&name=限时&type=0&status=0&sort=add_time&order=desc
     */

    @GetMapping("list")
    public BaseRespVo listCoupon(BaseParam baseParam,String name,Short status,Short type){
        BaseData data = couponService.listCoupon(baseParam,name,status,type);
        return BaseRespVo.ok(data);
    }

    /**
     * 可以查看某一个优惠券的详情
     * GET	http://182.92.235.201:8083/admin/coupon/read?id=14
     */

    @GetMapping("read")
    public BaseRespVo listReadCoupon(Integer id){
       Coupon coupon = couponService.listReadCoupon(id);
       return BaseRespVo.ok(coupon);
    }

    /**
     * 查看某一用户都有什么优惠券 GET
     * admin/coupon/listuser?page=1&limit=20&couponId=1&userId=1&status=0&sort=add_time&order=desc
     */
    @GetMapping("listuser")
    public BaseRespVo listUserCoupons(BaseParam baseParam,Integer couponId,Integer userId,Short status ){
        BaseData couponUserList= couponService.listUserCoupons(baseParam,couponId,userId,status);
        return BaseRespVo.ok(couponUserList);

    }

    /**
     * 新增coupon优惠券
     * POST admin/coupon/create
     */
    @MarketHibernateValid
    @PostMapping("create")
    public BaseRespVo createCoupon(@Validated @RequestBody CouponGoodsVO coupon, BindingResult bind){
        int rows = couponService.createCoupon(coupon);
        return BaseRespVo.ok(coupon);
    }

    /**
     * 更新优惠券
     * POST admin/coupon/update
     */
    @MarketHibernateValid
    @PostMapping("update")
    public BaseRespVo updateCoupon(@Validated @RequestBody CouponGoodsVO coupon, BindingResult bind){
        int rows = couponService.updateCoupon(coupon);
        return BaseRespVo.ok(coupon);
    }

    /**
     * POST  /admin/coupon/delete
     */
    @PostMapping("delete")
    public BaseRespVo deleteCoupon(@RequestBody Coupon coupon){
        int rows = couponService.deleteCoupon(coupon);
        return BaseRespVo.ok(null);
    }







}